package l2ft.gameserver.hwid;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.logging.FileHandler;
import java.util.logging.Handler;

import l2ft.gameserver.model.Player;
import l2ft.gameserver.utils.LogFormatter;

public class HwidGamer
{
	public final java.util.logging.Logger _hwidLog;
	
	public static enum PLAYER_THREAT {
		NONE,
		FRIENDLY,
		KEEP_EYE_ON,
		CRITICAL
	}
	private String _hwid;
	private List<Player> _onlineChars;
	private long _firstTimePlayed;
	private long _totalTimePlayed;
	private int _pollAnswer;
	private long _lastTimeVoted;//TODO
	private PLAYER_THREAT _threat;
	private boolean _isBanned;
	
	
	public HwidGamer(String hwid, long firstTimePlayed, long totalTimePlayed, int pollAnswer, PLAYER_THREAT threat, boolean banned)
	{
		_hwid = hwid;
		_firstTimePlayed = firstTimePlayed;
		_totalTimePlayed = totalTimePlayed;
		_pollAnswer = pollAnswer;
		_threat = threat;
		_onlineChars = new CopyOnWriteArrayList<>();
		_hwidLog = java.util.logging.Logger.getLogger(_hwid);
		_isBanned = banned;
		installHandler();
	}
	
	public void addPlayer(Player player)
	{
		System.out.println("adding player");
		_onlineChars.add(player);
	}
	
	public void removePlayer(Player player)
	{
		System.out.println("removing player");
		_onlineChars.remove(player);
		if(_onlineChars.isEmpty())
		{
			closeLogger();
			HwidEngine.getInstance().removeGamer(this);
		}
	}
	
	public List<Player> getOnlineChars()
	{
		return _onlineChars;
	}
	
	public String getHwid()
	{
		return _hwid;
	}
	
	public long getFirstTimePlayed()
	{
		return _firstTimePlayed;
	}
	
	public void incTotalTimePlayed(long timeToAdd)
	{
		_totalTimePlayed += timeToAdd;
	}
	
	public long getTotalTimePlayed()
	{
		return _totalTimePlayed;
	}
	
	public void setPollAnswer(int answer, boolean updateDb)
	{
		_pollAnswer = answer;
		if(updateDb)
		{
			System.out.println("updating in db, answer:"+_pollAnswer);
			HwidEngine.getInstance().updateGamerInDb(this);
		}
	}
	
	public int getPollAnswer()
	{
		System.out.println("poll answer:"+_pollAnswer);
		return _pollAnswer;
	}
	
	public PLAYER_THREAT getThreat()
	{
		return _threat;
	}
	
	public void setHwidBanned(boolean b)
	{
		_isBanned = b;
	}
	
	public boolean isHwidBanned()
	{
		return _isBanned;
	}
	
	/*
	 * Logging
	 */
	
	public java.util.logging.Logger getMyLogger()
	{
		return _hwidLog;
	}
	
	private void installHandler()
	{
		try
		{
			FileHandler handler = new FileHandler("log/"+_hwid+".log", true);
	        handler.setFormatter(new LogFormatter()); 
	        getMyLogger().addHandler(handler);
	        getMyLogger().setUseParentHandlers(false);
		}
		catch (SecurityException | IOException e)
		{
			e.printStackTrace();
		}
	}
	
	private void closeLogger()
	{
		for(Handler h : getMyLogger().getHandlers())
		{
		    h.close();
		}
	}
}
